/****************************************************************************
 **
 ** Trolltech hereby grants a license to use the Qt/Eclipse Integration
 ** plug-in (the software contained herein), in binary form, solely for the
 ** purpose of creating code to be used with Trolltech's Qt software.
 **
 ** Qt Designer is licensed under the terms of the GNU General Public
 ** License versions 2.0 and 3.0 ("GPL License"). Trolltech offers users the
 ** right to use certain no GPL licensed software under the terms of its GPL
 ** Exception version 1.2 (http://trolltech.com/products/qt/gplexception).
 **
 ** THIS SOFTWARE IS PROVIDED BY TROLLTECH AND ITS CONTRIBUTORS (IF ANY) "AS
 ** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 ** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
 ** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 ** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 ** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 ** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
 **
 ** Since we now have the GPL exception I think that the "special exception
 ** is no longer needed. The license text proposed above (other than the
 ** special exception portion of it) is the BSD license and we have added
 ** the BSD license as a permissible license under the exception.
 **
 ****************************************************************************/

#include "QTMarqueeListWidget.h"

QTMarqueeListWidget::QTMarqueeListWidget(QWidget *parent) :
	QListWidget(parent)
	{
	// initialize 
	speed = 2; // number of pixels to shift at each timeout
	direction = RightToLeft; // direction of the text movement
	delta = 0;
	
	timer_started = false;
	
	setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); // set Horizontal Scroll Bar to off
	
	connect(&timer, SIGNAL(timeout()), this, SLOT(refreshList()));
	connect(this, SIGNAL(currentRowChanged(int)), this,	SLOT(currentRowChangedSlot(int)));

	}

void QTMarqueeListWidget::refreshList()
	{
	// At each timeout repaint the List
	repaint();
	}

QTMarqueeListWidget::~QTMarqueeListWidget()
	{
	}

void QTMarqueeListWidget::show()
	{
	QListWidget::show();
	}

void QTMarqueeListWidget::paintEvent(QPaintEvent *evt)
	{

	QPainter p(viewport());
	// get viewport parameters and length of the text in Pixels
	viewport_rect = viewport()->rect();
	view_width = viewport_rect.width();
	
	p.save();

	QBrush item_brush(QColor(Qt::darkCyan), Qt::Dense4Pattern);
	textLength = fontMetrics().width(currentItem()->text());

	QListWidgetItem* currentItem;
	QModelIndex currentModelIndex;
	QRect currentrect;
	QString textToDraw;
	int xwidth;
	int ywidth;

	// for each item in the list box
	// 1. Get the Item Rectangle
	// 2. Calculate the new PX,PY for current focussed row,if the textlength exceeds screen width
	// 2a. For Non Focussed Displayed Items no need to calcualte new coordinates
	// 3. Draw the focus on the current  focussed row
	// 4. Check if item is within the Display area, no need to draw if item is not displayed
	// 5. Draw the Item
	for (int indx = 0; indx < count(); indx++)
		{
		currentItem = item(indx);
		currentModelIndex = indexFromItem(currentItem);
		currentrect = visualRect(currentModelIndex);

		xwidth = currentrect.width();
		ywidth = currentrect.height();
		px = currentrect.x();
		py = currentrect.y() + 25;
		if ((indx == currentRow()))
			{
			if (textLength > view_width)
				{
				switch (direction)
					{
					case LeftToRight:
						{
						if (delta > view_width)
							delta = -textLength;

						px = px + delta;
						delta = delta + speed;
						break;
						}
					case RightToLeft:
						{
						if (delta < -textLength)
							delta = view_width;

						px = px + delta;
						delta = delta - speed;
						break;
						}
					default:
						break;

					}

				}

			p.fillRect(currentrect, item_brush);
			}

		textToDraw = currentItem->text();

		// check if the item is Displayed in the screen
		bool m_display = isDisplayable(currentrect);
		if (m_display)
			p.drawText(px, py, textToDraw);

		}

	p.restore();

	if (!timer_started)
		{
		timer.start(100);
		timer_started = true;
		}

	}

void QTMarqueeListWidget::resizeEvent(QResizeEvent *evt)
	{
	QListWidget::resizeEvent(evt);
	}


void QTMarqueeListWidget::currentRowChangedSlot(int row)
	{
	// when ever any item is clicked running variable is reset
	delta = 0;
	}

int QTMarqueeListWidget::getSpeed()
	{
	return speed;
	}

void QTMarqueeListWidget::setDirection(int d)
	{
	direction = d;
	if (direction == RightToLeft)
		px = width() - textLength;
	else
		px = 0;
	refreshList();
	}

bool QTMarqueeListWidget::isDisplayable(QRect item_rect)
	{
	
	// check if the List Item is within the drawable window i.e ViewPort
	if ((item_rect.y() < viewport_rect.y()) || (item_rect.y()>= viewport_rect.height()))
		return false;

	return true;
	}

